#*-* utf-8 -*-
import numpy as np;
import scipy.sparse as sp
from multiprocessing import Pool
from itertools import izip_longest
import os
import utils
import sys
dx = 20134; 
dy = 20134;


def getColumnVector(s, d):
    row = [];
    col = [];
    data = [];
    li = s.strip().split(' ');
    for l in li:
        pair = l.strip().split(':');
        index = int(pair[0]);
        value = float(pair[1]);
        if index == 112897:
            continue;
        row.append(index-1);
        col.append(0);
        data.append(value)
    
    return sp.csc_matrix((data,(row, col)), shape=(d,1));

def sco(a, b):
    sum = 0.0;
    for c, d in zip(a,b):
        sum += c*d;
    return sum;
def score(x, y):
    print 'x, y',x, y;
    s1 = Lx.transpose().dot(x);
    print  's1', s1;
    s2 = Ly.transpose().dot(y);
    print 's2', s2;
    s = s1.transpose().dot(s2);
    print s;
    return s;
    
def grouper(n, iterable, padvalue=None):
    return izip_longest(*[iter(iterable)]*n, fillvalue=padvalue);

def processChunk(line):
    if not line:
        return None;
    #print 'pid:', os.getpid();
    li = line.split('\t');
    queryID = li[0];
    appID = li[1];
    if queryID in BM25Set:
        #return '%s\t%s\t%f\n' % (queryID, appID, float(li[4]));
        return None;
    queryFeature = getColumnVector(li[2], dx);
    documentFeature = getColumnVector(li[3], dy);
    #print queryFeature.shape, documentFeature.shape
    s = score(queryFeature, documentFeature);
    return '%s\t%s\t%f\n' % (queryID, appID, s.todense()[0][0]);

def getScore(line):
    li = line.split('\t');
    queryID = li[0];
    appID = li[1];
    queryFeature = getColumnVector(li[2], dx);
    documentFeature = getColumnVector(li[3], dy);
    s = score(queryFeature, documentFeature);
    return s;

def testing(LxFile, LyFile, resultFile):
    results = {};
    test = open('test.set', 'r');
    results_file = open(resultFile, 'w');

    line = test.readline();
    if not line.startswith('*'):
        print 'No Dimension Provided';
        return;
    global dx, dy;
    firstLi = line.split('\t');
    dx = int(firstLi[1]);
    dy = int(firstLi[2]);
    print 'dimension:', dx, dy;
    global Lx, Ly;
  

    #Lx = utils.load_sparse_lil('Lx.txt.npz').tocsr();
    #Ly = utils.load_sparse_lil('Ly.txt.npz').tocsr();
    Lx = utils.load_sparse_csr(LxFile);
    Ly = utils.load_sparse_csr(LyFile);
    
    print 'HAHAHAH', Lx.shape, Ly.shape;
    count = 0;
    K = 25;

    pool = Pool(processes=K);

    for chunk in grouper(K, test):
        results = pool.map(processChunk, chunk);
        for result in results:
                if result:
                    results_file.write(result);


    results_file.close();
    test.close();

queryMap = {};
docMap = {}
BM25Set= set();

def load_BM25Set():
    bm25 = open('bm25_query', 'r');
    for line in bm25:
        BM25Set.add(line.strip());

if __name__ == '__main__':
    LxFile = sys.argv[1];
    LyFile = sys.argv[2];
    resultFile = sys.argv[3];
    load_BM25Set();
    print LxFile, LyFile, resultFile;
    testing(LxFile, LyFile, resultFile);
